package com.informatics.polymer.server;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import com.informatics.polymer.server.Polymerisation.Dendrimerisation;
import com.informatics.polymer.server.Polymerisation.Homopolymerisation;
import com.informatics.polymer.server.Polymerisation.PolymerisationUtils;
/**
 * Restlet to implement dendrimerisation, it takes parameters from the GWT GUI then builds the
 * polymer using the polymerization API and returns the resultant XML to JMol for viewing.
 * @author ed
 * @version 1.0
 */

public class DendrimerBuilder extends Resource {
	/**
	 * String representation of the repeat unit.
	 */
	private String repeatUnit;
	/**
	 * Integer representation of the number of layers.
	 */
	private int layers;
	//Getters & Setters
	/**
	 * Sets the repeat unit.
	 * @param repeatUnitValue - smiles string for the repeat unit.
	 */
	private void setRepeatUnit(String repeatUnitValue){
		this.repeatUnit = repeatUnitValue;
	}
	/**
	 * Get repeat unit.
	 * @return String representation of the repeat unit.
	 */
	private String getRepeatUnit(){
		return repeatUnit;
	}
	/**
	 * Sets the number oflayers.
	 * @param layerValue - integer representing the number of layers to build the dendrimer.
	 */
	private void setNumberOfLayers(int layerValue){
		this.layers = layerValue;
	}
	/**
	 * Gets the number of layers.
	 * @return integer representing the number of layers in dendrimerisation.
	 */
	private int getNumberOfLayers(){
		return layers;
	}
	
	/**
	 * Initiate the restlet.
	 */
	public void init(Context context, Request request, Response response) {
		super.init(context, request, response);
	//	getVariants().add(Constants.XML_VARIANT);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
		this.setModifiable(true);
	}
	/**
	 * Method allows forms to be posted to restlet.
	 */
	@Override
	public boolean allowPost() {
		return true;
	}

	/**
	 * Constructs DendrimerBuilder resource.
	 * @param context 
	 * @param request
	 * @param response
	 */
	public DendrimerBuilder(Context context, Request request, Response response){
		super(context,request,response);
		getVariants().add(new Variant(MediaType.TEXT_PLAIN));
	}

	/**
	 * Method deals with the form posted to the restlet.
	 */
	@Override
	public void post(Representation entity) {
		Form form = new Form(entity); 
		setRepeatUnit(form.getValues("repeatUnit"));
		setNumberOfLayers(Integer.parseInt(form.getValues("layers")));
		String counterValue =  form.getValues("counter");
		Dendrimerisation dendrimerisation = new Dendrimerisation(getNumberOfLayers(),
				 getRepeatUnit());
		String dendrimerNoCoords = dendrimerisation.getPolymer().toXML();
		String polymerWith3DCoords = null;
/*		try {
			polymerWith3DCoords = PolymerisationUtils.add3DCoordinates(dendrimerNoCoords);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String finalResult = polymerWith3DCoords;
	*/
		StringRepresentation str = new StringRepresentation(dendrimerNoCoords);
		//Write out to a file
		String f = "/Dendrimer"+counterValue+".cml";
		String curDir = new String(System.getProperty("user.dir"));
		File dendrimerXml = new File(curDir+f);
		BufferedWriter out;
		//Use apache io
		try {
			FileUtils.writeStringToFile(dendrimerXml, dendrimerNoCoords);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			out = new BufferedWriter(new FileWriter(curDir+f));
			out.write(dendrimerNoCoords);
		    out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
       
		getResponse().setEntity(str);
	}

	// Respond to GET
	public Representation represent (Variant variant) throws ResourceException{
		Representation resource = null;
			if (!MediaType.TEXT_PLAIN.includes(variant.getMediaType())) {
				throw new ResourceException(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
			}
			else{
				StringBuffer sb = new StringBuffer();
				try {
					//sb.append("Created!");
					sb.append(":");
					sb.append(":");
				} catch (Exception e) {
					e.printStackTrace();
				}		
				resource = new StringRepresentation(sb.toString(), MediaType.TEXT_PLAIN); 
				return resource;
			}
	}

	
}
